package cn.otrue.patienthealthmanager.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.DensityUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;

import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * 聊天->评价医生
 * 
 * @author G
 * 
 */
public class DoctorEvaluationActivity extends BaseActivity {

	public void ImageSet(String imgurl, ImageView img_headPhoto) {

		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_user_photo)
				.showImageOnFail(R.drawable.iv_user_photo)
				.showImageForEmptyUri(R.drawable.iv_user_photo)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(
						new RoundedBitmapDisplayer(DensityUtil.dip2px(this, 40)))
				.build();
		ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	}

	private TextView tv_evaluation_account;
	private TextView tv_evaluation_inhospital;
	private TextView tv_evaluation_good;
	private RatingBar rb_evaluation_professional;
	private RatingBar rb_evaluation_altitue;
	private EditText et_evaluation_content;
	private TextView tv_evaluation_send;

	private Handler evaluationHandler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			if (state.equals("001")) {
				Toast.makeText(DoctorEvaluationActivity.this, "评价成功", 0).show();
				DoctorEvaluationActivity.this.finish();
			} else {
				Toast.makeText(DoctorEvaluationActivity.this, msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(DoctorEvaluationActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
	private Map<String, String> data;
	private ImageView iv_evaluation_image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doctor_evaluation);
		initView();

	}

	private void initView() {
		getData();
		iv_evaluation_image = (ImageView) findViewById(R.id.iv_evaluation_image);
		tv_evaluation_account = (TextView) findViewById(R.id.tv_evaluation_account);
		tv_evaluation_inhospital = (TextView) findViewById(R.id.tv_evaluation_inhospital);
		tv_evaluation_good = (TextView) findViewById(R.id.tv_evaluation_good);
		rb_evaluation_professional = (RatingBar) findViewById(R.id.rb_evaluation_professional);
		rb_evaluation_altitue = (RatingBar) findViewById(R.id.rb_evaluation_altitue);
		et_evaluation_content = (EditText) findViewById(R.id.et_evaluation_content);
		tv_evaluation_send = (TextView) findViewById(R.id.tv_evaluation_send);

		tv_evaluation_account.setText(data.get("realname"));
		tv_evaluation_inhospital.setText("医院:   " + data.get("inHospital"));
		tv_evaluation_good.setText("科室:   " + data.get("good_at_subject"));

		tv_evaluation_send.setOnClickListener(this);
		ImageSet(data.get("headimg"), iv_evaluation_image);
		initTitleBar();

	}

	private void getData() {
		Intent intent = getIntent();
		data = HttpServer.gson.fromJson(intent.getStringExtra("data"),
				new TypeToken<Map<String, String>>() {
				}.getType());

	}

	public void initTitleBar() {
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(getString(R.string.doctor_evaluation));
		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);

		tv_title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				DoctorEvaluationActivity.this.finish();

			}
		});

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_evaluation_send:

			String content = et_evaluation_content.getText().toString();
			if (!TextUtils.isEmpty(content)) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("sender", MyConst.getACCOUNT());
				map.put("receiver", data.get("doctorUser"));
				map.put("content", content);

				map.put("professional",
						(int) rb_evaluation_professional.getRating());
				map.put("altitue", (int) rb_evaluation_altitue.getRating());
				TransferParameter parameter = new TransferParameter();

				parameter.setData(map);
				new HttpServer().post(HttpCons.ADD_THE_DOCTOR_EVALUATION,
						parameter, evaluationHandler);
			} else {
				Toast.makeText(DoctorEvaluationActivity.this, "请输入评价内容", 0)
						.show();
			}

			break;

		default:
			break;
		}
	}
}
